package com.liuyjy.base.common.util;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Author liuyjy
 * @Date 2022/3/15 8:44
 * @Description: 加密对比
 **/
public class BCryptUtil {

    /**
     * 密码加密
     *
     * @param password 原始密码
     * @return String
     */
    public static String passwordEncode(String password) {
        return passwordEncode(password, 10);
    }

    /**
     * 密码加密 工作因子越大，生成的密码越复杂，但是加密速度越慢，一般 10-12 就可以了，
     *
     * @param password 原始密码
     * @param strength 工作因子 4-31
     * @return String
     */
    public static String passwordEncode(String password, int strength) {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(strength);
        return passwordEncoder.encode(password);
    }

    /**
     * 校验密码正确性，
     *
     * @param password 客户录入密码
     * @param dataPass 数据库保存密码
     * @return boolean
     */
    public static boolean match(String password, String dataPass) {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(password, dataPass);
    }


    public static void main(String[] args) {
        System.out.println(passwordEncode("123456", 10));

        System.out.println(match("123456", passwordEncode("123456")));

        String phpPassword = "$2y$13$OmJIzv2iNPwV5AANZkQ8fuhUW7/PbqvqG8zcHLlP2cZZf5jnQlR0K";
        String pswKey = "1234567890Aa";
        boolean match = BCryptUtil.match(pswKey, phpPassword);
        System.out.println(match);
        String token = TokenUtil.generateToken(1 + "");
        System.out.println(token);
    }
}
